<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

/**
 * 健康小屋逻辑
 */
class Healthroom extends LogicBase
{
    
    /**
     * 获取健康小屋列表
     */
    public function getHealthroomList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelHealthroom->getList($where, $field, $order, $paginate);
    }

    

    /**
     * 首页健康小屋数量统计
     */
    public function getHealthroomListForCount($where = [], $field = true, $order = '', $paginate = 2000000)
    {
        
        return $this->modelHealthroom->getList($where, $field, $order, $paginate);
    }
    /**
     * 卫健委获取健康小屋列表
     */
    public function getHealthroomListForCommission()
    {
        $type=session('department_info')['type'];
        $county_id=session('department_info')['county_id'];
        $this->modelHealthroom->alias('rm');
        $join=[['region r','r.id=rm.county_id'],['my_department d','d.id=rm.hospital_id']];
        $field='rm.*,d.name as hospitalname,r.name as countyname';
        $paginate=30;
              if($type==0 && $county_id=='130400')
            {
                $where=[];
                $order='rm.county_id,rm.town_id,rm.hospital_id';
            }elseif($type==0 && $county_id!='130400')
            {
                $where=['rm.county_id'=>$county_id];
                $order='rm.county_id,rm.town_id,rm.hospital_id';
            }else
            {
              return $this->modelHealthroom->getList(['id'=>-1]);
            }
            return $this->modelHealthroom->getList($where, $field, $order, $paginate,$join);
    }


    
    /**
     * 健康小屋信息编辑
     */
    public function healthroomEdit($data = [])
    {
        $data['room_id']=$data['county_id'];
        $data['town_id']=$data['city_id'];
        $data['county_id']=$data['province_id'];

        $validate_result = $this->validateHealthroom->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateHealthroom->getError()];
        }
        
        $url = url('healthroomList');
        
        $result = $this->modelHealthroom->setInfo($data);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelHealthroom->getError()];
    }

    /**
     * 获取个区县健康小屋数量
     */
    public function getHealthroomCount()
    {
        $this->modelHealthroom->alias('rm');
        $join=[['region r','r.id=rm.county_id','right']];
        $field='r.name name,count(rm.room_id) rmcount';
        $group='r.id';
        $where=['r.level'=>'1','r.id'=>['neq','130400']];
        $order='r.id';
        $list=$this->modelHealthroom->getList($where,$field,$order,20,$join,$group);
        return $list;
        
    } 

    /**
     * 首页拥有小屋医院总数
     */
    public function getHospitalListForCount($where = [], $field = true, $order = '', $paginate = 20000)
    {
       
      $field='count(distinct hospital_id) countnumber';
      $list=$this->modelHealthroom->getList($where,$field,$order,$paginate);
       return $list['0']['countnumber'];
    }

    /**
     * 获取全市小屋数量增长率
     */
    public function getHealthroomIncrease()
    {
        $field='start_time,count(room_id) rmcount';
        $group='start_time';
        $where=[];
        $join=[];
        $order='start_time';
        $list=$this->modelHealthroom->getList($where,$field,$order,20,$join,$group);
        sf($list);
        return $list;
        
    }

    /**
     * 获取小屋整体服务水平,各区县对照表
     */
    public function getHealthroomServiceLevel()
    {
        $this->modelActivity->alias('a');
        $join=[['my_healthroom rm','a.room_id=rm.room_id'],['region r','r.id=rm.county_id']];
        $field='r.name name,avg(attitude) attitude,avg(capacity) capacity,avg(quality) quality,avg(efficiency) efficiency,avg(discipline) discipline';
        $group='r.id';
        $where=['r.level'=>'1','r.id'=>['neq','130400']];
        $order='r.id';
        $list=$this->modelActivity->getList($where,$field,$order,20,$join,$group);
        return $list;
    }

    /**
     * 健康小屋信息编辑
     */
    public function healthroomAdd($data = [])
    {

        $data['room_id']=$data['county_id'];
        $data['town_id']=$data['city_id'];
        $data['county_id']=$data['province_id'];

        $validate_result = $this->validateHealthroom->scene('add')->check($data);
        
        if (!$validate_result) {
            
          return [RESULT_ERROR, $this->validateHealthroom->getError()];
        }
        
        $url = url('healthroomList');
                $func1 = function() use($data) {
            $this->modelExpertinroom->setInfo(['expert_id' =>$data['owner_code'],'room_id'=>$data['room_id'],'is_owner'=>1]);
             };
                
        $func2 = function()  use($data)
        { 
            $this->modelHealthroom->setInfo($data);
         };
        
      $result=closure_list_exe([$func1, $func2]);
  
        return  $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelHealthroom->getError()];
    }
    /**
     * 健康小屋信息编辑
     */
    public function introEdit($data = [])
    {

        $url = url('hospital/healthroomList');
        $data['description'] = html_entity_decode($data['description']);

        $result = $this->modelHealthroom->setInfo($data,['room_id'=>$data['room_id']]);
        
        $handle_text = '修改概况';
        
        $result && action_log($handle_text, '健康小屋' . $handle_text . ',room_id：' . $data['room_id']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelHealthroom->getError()];
    }
    /**
     * 获取健康小屋信息
     */
    public function getHealthroomInfo($where = [], $field = true)
    {
        
        return $this->modelHealthroom->getInfo($where, $field);
    }
    
    /**
     * 健康小屋删除
     */
    public function healthroomDel($where = [])
    {
        
        $result = $this->modelHealthroom->deleteInfo($where);
        
        $result && action_log('删除', '健康小屋删除' . '，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelHealthroom->getError()];
    }

     /**
     * 健康小屋数据导入
     */
     public function healthroomImport($test_url = 'd:\\healthroom.xlsx')
     {

       $url = url('healthCommission/healthroomList');
       $data = get_excel_data($test_url);


       $list=arrToList($data,['name','code','start_time','owner_name','owner_code','address','telphone','hospital_code','region_code','county_id','description','star_level','disease_type','longitude','latitude']);
            //定义变量跟踪多数据验证过程
       $is_validate_error=false;
       foreach($list as $rr){
        $validate_result = $this->validateHealthroom->scene('import')->check($rr);
        if (!$validate_result) {
               // return [RESULT_ERROR, $this->validateDepartment->getError()];
            $is_validate_error=true;
        }
    }
    if(!$is_validate_error){
        $result= $this->modelHealthroom->setList($list);
    }
    else
    {
        $result=false;
        $this->error='批量导入时数据验证失败，请检测数据后重试';
    }
       return $result ? [RESULT_SUCCESS, '导入成功',$url] : [RESULT_ERROR,$this->error];
}
 
}
